﻿using Microsoft.Extensions.Configuration;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace EnterpriseWebsiteUtil.Helper
{
    public class SqlSugarHelper : IDisposable
    {
        public SqlSugarClient sugarClient { set; get; }

        public readonly IConfiguration config;

        public SqlSugarHelper(IConfiguration config)
        {
            this.config = config;
            var connection = config["MMSQLConnection"];
            if (string.IsNullOrEmpty(connection))
                throw new Exception("请配置MMSQLConnection字段");
            sugarClient = new SqlSugarClient(
                new ConnectionConfig()
                {
                    ConnectionString = connection,
                    DbType = DbType.SqlServer,//设置数据库类型
                    IsAutoCloseConnection = true,//自动释放数据务，如果存在事务，在事务结束后释放
                    InitKeyType = InitKeyType.Attribute //从实体特性中读取主键自增列信息
                });
        }

        public string GetConnection()
        {
            return config["MMSQLConnection"];
        }

        public void Dispose()
        {
            sugarClient.Dispose();
        }
    }
}
